﻿using AutoMapper;
using Domain.Process_Procedures;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Process_Procedures.ProcessManagement;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Process_Procedures.ProcessManagement
{
    public class UpdateProcessManagementHandler : IRequestHandler<UpdateProcessManagementCommand, ApiResult<int>>
    {
        private readonly IRepository<ProcessManagementModel> processmanRepo;
        private readonly IMapper mapper;

        public UpdateProcessManagementHandler(IRepository<ProcessManagementModel> processmanRepo, IMapper mapper)
        {
            this.processmanRepo = processmanRepo;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(UpdateProcessManagementCommand request, CancellationToken cancellationToken)
        {
            var processman = await processmanRepo.GetValue(request.Id);
            if (processman == null)
            {
                return new ApiResult<int>().Fail("工序管理不存在");
            }
            processman = mapper.Map(request, processman);
            var r= await processmanRepo.UpdateAsync(processman);
            if (r > 0)
            {
                return new ApiResult<int>().Success("更新成功", r);
            }
            else
            {
                return new ApiResult<int>().Fail("更新失败");
            }

        }
    }
}
